<template>
    <div class="min-h-svh w-full bg-gradient-to-br from-gray-100 via-gray-200 to-green-100 dark:from-slate-900 dark:via-slate-800 dark:to-green-900 px-[2%] relative overflow-hidden">
        <!-- 淡化的叶子图案背景 -->
        <div class="absolute inset-0 bg-repeat opacity-3 dark:opacity-5" style="background-image: url('data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'0 0 80 40\' width=\'80\' height=\'40\'%3E%3Cpath fill=\'%2322c55e\' fill-opacity=\'0.2\' d=\'M0 40a19.96 19.96 0 0 1 5.9-14.11 20.17 20.17 0 0 1 19.44-5.2A20 20 0 0 1 20.2 40H0zM65.32.75A20.02 20.02 0 0 1 40.8 25.26 20.02 20.02 0 0 1 65.32.76zM.07 0h20.1l-.08.07A20.02 20.02 0 0 1 .75 5.25 20.08 20.08 0 0 1 .07 0zm1.94 40h2.53l4.26-4.24v-9.78A17.96 17.96 0 0 0 2 40zm5.38 0h9.8a17.98 17.98 0 0 0 6.67-16.42L7.4 40zm3.43-15.42v9.17l11.62-11.59c-3.97-.5-8.08.3-11.62 2.42zm32.86-.78A18 18 0 0 0 63.85 3.63L43.68 23.8zm7.2-19.17v9.15L62.43 2.22c-3.96-.5-8.05.3-11.57 2.4zm-3.49 2.72c-4.1 4.1-5.81 9.69-5.13 15.03l6.61-6.6V6.02c-.51.41-1 .85-1.48 1.33zM17.18 0H7.42L3.64 3.78A18 18 0 0 0 17.18 0zM2.08 0c-.01.8.04 1.58.14 2.37L4.59 0H2.07z\'%3E%3C/path%3E%3C/svg%3E');">
        </div>

        <div class="relative z-10 flex flex-col justify-center space-y-4">
            <header class="text-2xl font-semibold flex justify-between items-center pt-4 text-gray-800 dark:text-gray-100">
                <span>个人中心</span>
                <div class="flex items-center justify-center gap-2">
                    <userUpdate/>
                    <passwordUpdate>
                        <template #triger>
                            <Button class="bg-green-600 hover:bg-green-700 text-white">修改密码</Button>
                        </template>
                    </passwordUpdate>
                    <addressNav/>
                </div>
            </header>
            <div class="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
                <Card class="space-y-1 bg-white dark:bg-slate-800 shadow-md">
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-gray-700 dark:text-gray-300">
                            碳积分
                        </CardTitle>
                        <Award class="h-4 w-4 text-green-600 dark:text-green-400"></Award>
                    </CardHeader>
                    <CardContent class="space-y-1">
                        <div class="text-xl font-bold text-gray-800 dark:text-gray-200">
                            {{ userPoints }}
                        </div>
                        <p class="text-xs text-green-600 dark:text-green-400">
                            可通过交易物品获得积分奖励。
                        </p>
                    </CardContent>
                </Card>
                <Card @click="router.push('/orderCenter')" class="space-y-1 cursor-pointer bg-white dark:bg-slate-800 shadow-md">
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-gray-700 dark:text-gray-300">
                            我买到的
                        </CardTitle>
                        <Award class="h-4 w-4 text-green-600 dark:text-green-400"></Award>
                    </CardHeader>
                    <CardContent class="space-y-1">
                        <div class="text-xl font-bold text-gray-800 dark:text-gray-200">
                            {{ buy }}件
                        </div>
                        <p class="text-xs text-green-600 dark:text-green-400">
                            点击查看详情
                        </p>
                    </CardContent>
                </Card>
                <Card @click="router.push('/sellerOrder')"  class="space-y-1 cursor-pointer bg-white dark:bg-slate-800 shadow-md">
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-gray-700 dark:text-gray-300">
                            我卖出的
                        </CardTitle>
                        <Award class="h-4 w-4 text-green-600 dark:text-green-400"></Award>
                    </CardHeader>
                    <CardContent class="space-y-1">
                        <div class="text-xl font-bold text-gray-800 dark:text-gray-200">
                            {{ sell }}件
                        </div>
                        <p class="text-xs text-green-600 dark:text-green-400">
                            点击查看详情
                        </p>
                    </CardContent>
                </Card>
                <Card @click="router.push('/pointStore')"  class="space-y-1 cursor-pointer bg-white dark:bg-slate-800 shadow-md">
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-gray-700 dark:text-gray-300">
                            积分商城
                        </CardTitle>
                        <Award class="h-4 w-4 text-green-600 dark:text-green-400"></Award>
                    </CardHeader>
                    <CardContent class="space-y-1">
                        <div class="text-xl font-bold text-gray-800 dark:text-gray-200">
                            查看积分商品。
                        </div>
                        <p class="text-xs text-green-600 dark:text-green-400">
                            点击查看详情
                        </p>
                    </CardContent>
                </Card>
            </div>
            <div class="grid gap-4 md:grid-cols-2 lg:grid-cols-7">
                <mypublish class="col-span-4  rounded-lg"/>
                <collection class="col-span-4 md:col-span-3  rounded-lg"/>
            </div>
        </div>
    </div>
</template>

<script setup>
import { Button } from '@/components/ui/button';
import { Card, CardTitle, CardHeader, CardContent, CardDescription } from '@/components/ui/card';
import {
    Avatar,
    AvatarFallback,
    AvatarImage,
} from '@/components/ui/avatar'
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuItem,
    DropdownMenuLabel,
    DropdownMenuSeparator,
    DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Award } from 'lucide-vue-next';
import userUpdate from '@/components/userUpdate.vue'
import passwordUpdate from '@/components/passwordUpdate.vue';
import addressNav from '@/components/addressNav.vue';
import collection from '@/components/collection.vue';
import mypublish from '@/components/mypublish.vue';

import { useUser } from '@/utils/pinia'
import { onMounted, ref } from 'vue';
import api from '@/utils/api'
import { success, warning } from '@/utils/message'
import { useRouter } from 'vue-router';

// 页面数据
const userPoints = ref(0)
const user = useUser()
const file = ref(null)
const buy = ref(0)
const sell = ref(0)
const userInfo = ref({
    username: '',
    avatar: '',
})
const router = useRouter()

onMounted(() => {
    userInfo.value = { ...user.getUser() }
    getPoint()
    getAllOrder(1,buy)
    getAllOrder(2,sell)
})

const getPoint = () => {
  api({
    url: "/user/user/getPoint",
    method: "GET",
    params: {
      id: user.getUser().id
    }
  }).then(res => {
    userPoints.value = res.data
  })
}

const getAllOrder = async (buy,value) => {
  let data = {
    page: 1,
    pageSize: 10,
    status: 4,
    title: null,
    buyOrSell: buy,
    productType: 1
  }
  await api({
    url: '/user/order/page',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: data
  }).then(res => {
    if (res.code === 1) {
      value.value = res.data.total
    }
  })
}

</script>